package com.ru.application.creditscore.service;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import com.ru.application.creditscore.dao.ScoringFactorDao;
import com.ru.application.creditscore.dao.support.SearchMode;
import com.ru.application.creditscore.dao.support.SearchParameters;
import com.ru.application.creditscore.domain.ScoringFactor;

public class ScoringFactorServiceImpl implements ScoringFactorService {
    private final static Logger logger = Logger.getLogger(ScoringFactorServiceImpl.class);
    private ScoringFactorDao factorDao;
    
    public ScoringFactor findFactor(Integer id) {
	if (logger.isDebugEnabled()) {
		logger.debug("Find factor with Id = " + id.toString());
	}
	ScoringFactor factor = new ScoringFactor();
	try {
	        factor.setId(id);
		SearchParameters sp = new SearchParameters();
		sp.searchMode(SearchMode.EQUALS);
		factor = factorDao.findUnique(factor, sp);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Find factor OK with Id = " + id.toString());
	}
	return factor;
    }
    
    public List<ScoringFactor> findAll(SearchParameters sp) {
	if (logger.isDebugEnabled()) {
		logger.debug("Find all factor");
	}
	List<ScoringFactor> listFactor = new ArrayList<ScoringFactor>();
	ScoringFactor factor = new ScoringFactor();
	try {
	    listFactor = factorDao.find(factor, sp);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Find all factor OK");
	}
	return listFactor;
    }
    
    @Transactional
    public boolean updateFactor(ScoringFactor pFactor) {
	if (logger.isDebugEnabled()) {
		logger.debug("Update factor with id = "
				+ pFactor.getId().toString());
	}
	boolean result = false;
	ScoringFactor factor = new ScoringFactor();
	try {
	    factor = factorDao.merge(pFactor);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled() && factor != null) {
		logger.debug("Update factor OK");
		result = true;
	}
	return result;
    }
    
    @Transactional
    public void createFactor(ScoringFactor factor) {
	if (logger.isDebugEnabled()) {
		logger.debug("create factor");
	}

	try {
	    factorDao.save(factor);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("create factor OK");
	}
    }
    
    @Transactional
    public void removeFactor(Integer id) {
	if (logger.isDebugEnabled()) {
		logger.debug("remove factor");
	}
	ScoringFactor factor = new ScoringFactor();
	factor.setId(id);
	try {
	    factorDao.delete(factor);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("remove factor OK");
	}
    }
    
    @Transactional
    public void updateListFactor(List<ScoringFactor> listFactor) {
	if (logger.isDebugEnabled()) {
		logger.debug("factor: updateListSchema.");
	}

	try {
		for (ScoringFactor factor : listFactor)
		    factorDao.merge(factor);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("factor: updateListFactor OK");
	}
    }
    
    @Transactional
    public void createListFactor(List<ScoringFactor> listFactor) {
	if (logger.isDebugEnabled()) {
		logger.debug("factor: createListFactor.");
	}

	try {
		for (ScoringFactor factor : listFactor)
		    factorDao.save(factor);
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("factor: createListFactor OK");
	}
    }
    
    @Transactional
    public void removeListFactor(List<Integer> listId) {
	if (logger.isDebugEnabled()) {
		logger.debug("factor: removeListFactor.");
	}

	try {
		for (Integer id : listId) {
		        ScoringFactor factor = new ScoringFactor();
		        factor.setId(id);
			factorDao.delete(factor);
		}
	} catch (Exception e) {
		logger.error("Error: " + e.toString());
		throw new ServiceException(ServiceException.LIB_JPA_PB,
				ServiceException.CODE_JPA_PB, e);
	}
	if (logger.isDebugEnabled()) {
		logger.debug("factor: removeListFactor OK");
	}
    }

    public ScoringFactorDao getFactorDao() {
        return factorDao;
    }

    public void setFactorDao(ScoringFactorDao factorDao) {
        this.factorDao = factorDao;
    }
  
}
